package com.ly.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ly.service.ShopKeeperService;
import com.system.comm.util.JsonUtils;
import com.system.common.pojo.BisShopUser;
import com.system.common.pojo.SysUser;

@Controller
@RequestMapping("ly")
public class ShopKeeperController {
	@Autowired
	private ShopKeeperService shopkeeperservice;

	@RequestMapping("shoplist")
	public String shoplist(Model model, HttpServletRequest request) {
		Map<String,Object> map = initMap(request);// 从前台获取的值
		System.out.println(map);
		List<BisShopUser> shoplist = shopkeeperservice.shoplist(map);
		model.addAttribute("shoplist", shoplist);
		return "ly/shopkeeperList";
	}

	// 获取前台的值
	private Map<String,Object> initMap(HttpServletRequest request) {
		Map<String,Object> map = new HashMap<String,Object>();
		String account = request.getParameter("account");
		String username = request.getParameter("username");
		String status = request.getParameter("status");
		String mobile = request.getParameter("mobile");
		String idno = request.getParameter("card");
		String email = request.getParameter("email");

		if (account != null ) {
			map.put("account", account);
		}

		if (username != null ) {
			map.put("username", username);
		}

		if (status != null ) {
			map.put("status", status);
		}

		if (mobile != null ) {
			map.put("mobile", mobile);
		}

		if (idno != null ) {
			map.put("idno", idno);
		}
		if (email != null ) {
			map.put("email", email);
		}
		return map;
	}
	
	/**
	 * 添加店铺信息
	 * @param bisShopUser
	 * @return
	 */
	@RequestMapping("saveshopkeeper")
	public String saveshopkeeper(BisShopUser bisShopUser,HttpSession session){	
		SysUser user=(SysUser) session.getAttribute("user");
		bisShopUser.setCreateUserId(user.getUserId());
		bisShopUser.setIsDel("0");
		shopkeeperservice.add(bisShopUser);
		return "redirect:/ly/shoplist";
	}
	
	/**
	 * 根据主键查询
	 * @param model
	 * @param shopId
	 * @return
	 */
	@RequestMapping("selectshopbyshopuserid")
	public String selectshopbyshopuserid(Model model,String shopUserId){
		BisShopUser shopUser=shopkeeperservice.selectshopbyshopuserid(shopUserId);
		model.addAttribute("shopUser", shopUser);
		return null;
	}
	
	
	
	/**
	 * 修改前给模态框赋值
	 * @param shopId
	 * @return
	 */
	@RequestMapping(value="toupdateshopkeeper",produces="application/json;charset=utf-8")
	@ResponseBody
	public String toupdate(String shopUserId){
		BisShopUser bisShopUser=shopkeeperservice.selectshopbyshopuserid(shopUserId);//调用根据id查询的店铺id方法
		String bisshopuser=JsonUtils.toJsonString(bisShopUser);//将对象转换成字符串
		return bisshopuser;//返回json数据
	
	}
	/**
	 * 修改店鋪信息
	 * @param bisShop
	 * @param request
	 * @param file
	 * @return
	 */
	
	@RequestMapping("updateshopkeeper")
	public String updateshopkeeper(BisShopUser bisShopUser){	
		bisShopUser.setIsDel("0");
		shopkeeperservice.update(bisShopUser);
		return "redirect:/ly/shoplist";
	}
	//删除店铺信息
	@RequestMapping("deleteshopkeeper")
	public String deleteshopkeeper(String shopUserId){
		shopkeeperservice.delete(shopUserId);
		return "redirect:/ly/shoplist";
	}
	
	/**
	 * 员工信息>>>>>>修改密码
	 * @return
	 */
	@RequestMapping("user_update_password")
	public String user_update_password(String shopUserId,String password2){
		BisShopUser shopUser=shopkeeperservice.selectshopbyshopuserid(shopUserId);
		shopUser.setPassword(password2);
		shopkeeperservice.update(shopUser);
		return "redirect:/ly/shoplist";
	}
	
	
	
	
	
	
}
